package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdChannelMapper;
import com.usian.admin.service.AdChannelService;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannelServiceImpl extends BaseServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        //1.参数检测
        if (dto == null)return errorParam;
        LambdaQueryWrapper<AdChannel> qw = new LambdaQueryWrapper<>();
        //判空
        if (StringUtils.isNotBlank(dto.getName())) {
            qw.like(AdChannel::getName, dto.getName());
        }
        return getPage(dto,qw);
    }

    @Override
    public ResponseResult saveAdChannel(AdChannel adChannel) {
        if (adChannel==null)return errorParam;
        adChannel.setCreatedTime(new Date());
        save(adChannel);
        return success;
    }

    @Override
    public ResponseResult updateAdChannel(AdChannel adChannel) {
        if (adChannel==null)return errorParam;
        updateById(adChannel);
        return success;
    }

    @Override
    public ResponseResult del(Integer id) {
        if (id==null)return errorParam;
        AdChannel channel = getById(id);
        if (channel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (channel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道有效不能删除");
        }
        int a = 1/0;
        removeById(id);

        return success;
    }

}
